<?php 

class Ext_Controller_Plugin_Debug extends Zend_Controller_Plugin_Abstract
{
       /**
      * Constructor
      *
      * @return void
      **/
       public function __construct()
       {
       }

       public function preDispatch(Zend_Controller_Request_Abstract $request)
       {
       	
			$front = Zend_Controller_Front::getInstance();
			$bootstrap = $front->getParam('bootstrap');
			$options = $bootstrap->getApplication()->getOptions();

			if ($options['zfdebug']['open'])
	        {
	        	$autoloader = Zend_Loader_Autoloader::getInstance();
			    $autoloader->registerNamespace('ZFDebug');
			    
			    $options = array(
			        'plugins' => array('Variables', 
			                           'File'=>array('base_path'=>realpath(APPLICATION_PATH.'/../')),
			                           'Memory', 
			                           'Time', 
			                           'Registry', 
			                           'Exception')
			    );
			    /*
				# Instantiate the database adapter and setup the plugin.
			    # Alternatively just add the plugin like above and rely on the autodiscovery feature.
			    if (Zend_Registry::isRegistered('_use_adapter')) {
			        //$bootstrap->bootstrap('db');
			        //$db = $bootstrap->getPluginResource('db')->getDbAdapter();
			        
			    	$_use_adapter = Zend_Registry::get('_use_adapter');
			        $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
			        $options['plugins']['Database']['adapter'] = $db;
			    }*/
			    
			    $_use_adapter = "master";
			    $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
			    $options['plugins']['Database']['adapter'][] = $db;
			    $_use_adapter = "slave";
			    $db = Zend_Registry::get('dataSource')->getConnection($_use_adapter);
			    $options['plugins']['Database']['adapter'][] = $db;
			    
			    if ($bootstrap->hasPluginResource('cache')) {
			        $bootstrap->bootstrap('cache');
			        $cache = $bootstrap-getPluginResource('cache')->getDbAdapter();
			        $options['plugins']['Cache']['backend'] = $cache->getBackend();
			    }
			    
			    $debug = new ZFDebug_Controller_Plugin_Debug($options);
			    
			    $bootstrap->bootstrap('frontController');
			    $frontController = $bootstrap->getResource('frontController');
			    $frontController->registerPlugin($debug);
	        }
       }
}
